package com.aigou.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aigou.exception.AigouException;
import com.aigou.pojo.Goods;
import com.aigou.pojo.Result;
import com.aigou.service.GoodsService;
import com.aigou.utils.AiGouUtil;
import com.aigou.vo.CartVo;
import com.aigou.vo.GoodsVo;
import com.alibaba.fastjson2.JSON;

@RestController
@RequestMapping("/cart")
public class CartController {
	
	@Autowired
	StringRedisTemplate redisTemplate;
	@Autowired
	GoodsService goodsService;
	@Autowired
	AiGouUtil aiGouUtil;

	@GetMapping(value = "/list")
    public Result cartList() {
		Result result = new Result();
		return result;
	}
	
	@PostMapping(value = "/add")
	public Result add(Integer gid, Integer num, Integer uid) {
		
		if (gid == null) {
			throw new AigouException("请选择商品");
		}
		
		// redis中根据用户id获取购物车
		String cartStr = redisTemplate.opsForValue().get(String.valueOf(uid));
		CartVo cartVo = null;
		if (cartStr != null) {
			cartVo = JSON.parseObject(cartStr, CartVo.class);
		} else {
			// 如果购物车为空就创建
			cartVo = new CartVo();
			cartVo.setGoodsList(new java.util.concurrent.CopyOnWriteArrayList<>());
			cartVo.setCreateTime(new Date());
			cartVo.setId(aiGouUtil.createOrderNum("XS"));
			cartVo.setUid(uid);
		}
		// 循环获取商品是否存在购物车
		List<GoodsVo> goodsList = cartVo.getGoodsList();
		boolean flag = false;
		GoodsVo goodsVo = null;
		for (GoodsVo g : goodsList) {
			if (gid.equals(g.getGid())) {
				flag = true;
				goodsVo = g;
				break;
			}
		}
		// 如果商品存在购物车就累加数量
		if (flag) {
			if (goodsVo.getGoodsNum() < goodsVo.getNum() + num) {
				throw new AigouException("商品库存不足");
			}
			goodsVo.setNum(goodsVo.getNum() + num);
		} else {
			goodsVo = new GoodsVo();
			Goods goods = goodsService.findOne(gid);
			if (goods == null) {
				throw new AigouException("商品不存在");
			}
			if (goods.getNum() < num) {
				throw new AigouException("商品库存不足");
			}
			goodsVo.setGid(gid);
			goodsVo.setNum(num);
			goodsVo.setGname(goods.getName());
			goodsVo.setPrice(goods.getSellPrice());
			goodsVo.setGoodsNum(goods.getNum());
			goodsList.add(goodsVo);
		}
		
//		BigDecimal totalPrice = new BigDecimal(0); 
		// 循环商品价格乘以数量再累加
//		for (GoodsVo gv : cartVo.getGoodsList()) {
//			BigDecimal price = gv.getPrice().multiply(new BigDecimal(gv.getNum()));
//			totalPrice = totalPrice.add(price);
//		}
		//计算商品总价
		Optional<BigDecimal> optional = cartVo.getGoodsList().stream().map((g) -> {
			if (g.getPrice() == null) {
				g.setPrice(new BigDecimal(0));
			}
			return g.getPrice().multiply(new BigDecimal(g.getNum()));
		}).reduce((x,y) -> x.add(y));
		cartVo.setTotalPrice(optional.get());

		// 存入redis
		redisTemplate.opsForValue().set(String.valueOf(uid), JSON.toJSONString(cartVo));
		Result result = new Result();
		result.setData(cartVo);
		return result;
	}
	
}
